package com.epfresh.supplier.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.epfresh.api.permission.HiPermission;
import com.epfresh.api.permission.PermissionCallback;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.SortModel;
import com.epfresh.supplier.fragment.CustomerContactFragment;
import com.epfresh.supplier.widget.*;
import java.util.ArrayList;

public class CustomerInfoActivity extends RequestCustomerListActivity implements ViewVariable {

    private LinearLayout mLlNoCustomers;
    private LinearLayout mLlHasCustomers;
    private CustomerContactFragment mCustomerContactFragment;
    private String name;
    private String mGroupId;
    private LinearLayout mRlGroups;
    private String mAction;
    private ArrayList<SortModel> mCustomer = new ArrayList<>();
    private String mFilter;
    private EditText mEtSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_info);
        initView();
        initFragment();
    }

    @Override
    protected View getToolBar() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCustomerList("1", name, mGroupId);//// TODO: 2017/9/19
    }

    private void initFragment() {
        mCustomerContactFragment = CustomerContactFragment.newInstance();

        if (!mCustomerContactFragment.isAdded()) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_customer_contact, mCustomerContactFragment, "mCustomerContactFragment")
                    .commit();
        }
    }


    private void initView() {
        ImageView iv = (ImageView) findViewById(R.id.iv_customer_info_add);
        iv.setOnClickListener(this);
        mAction = getIntent().getAction();
        mLlNoCustomers = (LinearLayout) findViewById(R.id.ll_customer_nobody);
        mLlHasCustomers = (LinearLayout) findViewById(R.id.ll_customer_somebody);
        mRlGroups = (LinearLayout) findViewById(R.id.rl_customer_groups);
        mEtSearch = (EditText) findViewById(R.id.et_customer_search);

        if (BILLING_CHECK_ACTIVITY.equals(mAction)) {
            mRlGroups.setVisibility(View.GONE);
            mGroupId = getIntent().getStringExtra("groupId");
        } else if (CART_ACTIVITY.equals(mAction)) {
            mGroupId = getIntent().getStringExtra("groupId");
            if (mGroupId == null || "".equals(mGroupId)) {
                mRlGroups.setVisibility(View.GONE);
            }

        }
        if (STANDING_BOOK_FILTER_FRAGMENT.equals(mAction)) {
            mFilter = getIntent().getStringExtra("filter");
        }
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mCustomerContactFragment.transferFilterString(editable.toString(), CustomerInfoActivity.this);
            }
        });
        findViewById(R.id.back_ll).setOnClickListener(this);
        findViewById(R.id.rl_customer_groups).setOnClickListener(this);
        findViewById(R.id.tv_add_new).setOnClickListener(this);
        findViewById(R.id.tv_add_book).setOnClickListener(this);
    }

    @Override
    protected void updateUI(ArrayList<SortModel> mCustomerItem) {
        mCustomer = mCustomerItem;
        if (null == mCustomerItem || 0 == mCustomerItem.size()) {
            mLlNoCustomers.setVisibility(View.VISIBLE);
            mLlHasCustomers.setVisibility(View.GONE);
        } else {
            mLlNoCustomers.setVisibility(View.GONE);
            mLlHasCustomers.setVisibility(View.VISIBLE);
            mCustomerContactFragment.transferData(mCustomerItem, mAction);
            if (!TextUtils.isEmpty(mFilter)) {
                mEtSearch.setText(mFilter);
                mCustomerContactFragment.transferFilterString(mFilter, this);
            }
        }
        if (groupSize>1) {
            mRlGroups.setVisibility(View.VISIBLE);
        } else {
            mRlGroups.setVisibility(View.GONE);
        }
    }
    StringBuffer phoneList = new StringBuffer();
    @Override
    public void onClick(View v){
        super.onClick(v);
        Intent main;
        switch(v.getId()){
            case R.id.iv_customer_info_add:
                if(findViewById(R.id.ll_add).getVisibility()==View.GONE){
                    findViewById(R.id.ll_add).setVisibility(View.VISIBLE);
                }else {
                    findViewById(R.id.ll_add).setVisibility(View.GONE);
                }
                break;
            case R.id.tv_add_book:
                HiPermission.create(this)
                        .checkSinglePermission(Manifest.permission.READ_CONTACTS, new PermissionCallback() {
                            @Override
                            public void onClose() {

                                T.toast("onClose");
                            }

                            @Override
                            public void onFinish() {

                                T.toast("onFinish");
                            }

                            @Override
                            public void onDeny(String permisson, int position) {
                                T.toast("onDeny");
                            }

                            @Override
                            public void onGuarantee(String permisson, int position) {
                                Intent main = new Intent(CustomerInfoActivity.this, ContactsActivity.class);
                                for(int i=0;i<mContact.size();i++){
                                    phoneList.append(mContact.get(i).getPhone()+",");
                                }
                                main.putExtra("phoneList",phoneList.toString());
                                startActivityForResult(main,0x001);
                                findViewById(R.id.ll_add).setVisibility(View.GONE);
                            }
                        });
                break;
            case R.id.tv_add_new:
                findViewById(R.id.ll_add).setVisibility(View.GONE);
                main = new Intent(this, CustomerEditAddActivity.class);
                main.setAction(CUSTOMER_INFO_ACTIVITY);
                main.putExtra("enterCustomerActivity", mAction);
                main.putExtra("groupSize",groupSize);
                startActivityForResult(main,0x001);
                break;
            case R.id.back_ll:
                mCustomerContactFragment.setUpdateAdapter(false);
                finish();
                break;
            case R.id.rl_customer_groups:
                Intent group = new Intent(this,CustomerGroupActivity.class);
                group.setAction(CUSTOMER_INFO_ACTIVITY) ;
                group.putExtra("enterCustomerActivity", mAction);//记录是从哪个界面进入客户列表
                startActivity(group);
                break;
        }
    }

    public void updateFilterUI(ArrayList<SortModel> filterDateList) {
        if (null == filterDateList || 0 == filterDateList.size()) {
            mLlNoCustomers.setVisibility(View.VISIBLE);
            mLlHasCustomers.setVisibility(View.GONE);
        } else {
            mLlNoCustomers.setVisibility(View.GONE);
            mLlHasCustomers.setVisibility(View.VISIBLE);
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(resultCode == 0x001)
            getCustomerList(null,"","");
    }
}
